﻿// -------------------------------------------------------------------------
//    @FileName         :    NFWatchTime.cpp
//    @Author           :    gaoyi
//    @Date             :    24-8-8
//    @Email            :    445267987@qq.com
//    @Module           :    NFWatchTime
//
// -------------------------------------------------------------------------

#include "NFWatchTime.h"

#ifdef NF_DEBUG_MODE
bool NFWatchTime::ms_bNeedRecordTime = true;
bool NFModuleWatchTime::ms_bNeedRecordTime = true;
#else
bool NFWatchTime::ms_bNeedRecordTime = false;
bool NFModuleWatchTime::ms_bNeedRecordTime = false;
#endif

NFWatchTime::NFWatchTime(int iID, int iSubID, int iByte, ENetStatisticType eType, int* piRetCode)
	: m_iID(iID),
	  m_iSubID(iSubID),
	  m_iByte(iByte),
	  m_eNetStaticsticType(eType),
	  m_piRetCode(piRetCode)
{
	if (ms_bNeedRecordTime)
	{
		m_stBegin = NFGetTime();
	}
}

NFWatchTime::~NFWatchTime()
{
	int iPassedTimeUs = 0;

	if (ms_bNeedRecordTime)
	{
		iPassedTimeUs = NFGetTime() - m_stBegin;

		if (iPassedTimeUs < 0)
		{
			LOGSVR_ERROR("iPassedTimeUs Is Invalid:" << iPassedTimeUs << " ID:" << m_iID);
			iPassedTimeUs = 0;
		}
	}

	int iRetCode = 0;
	if (m_piRetCode != NULL)
	{
		iRetCode = *m_piRetCode;
	}

	NFOperatingStatistcsMgr::Instance()->AddMsgStat(m_iID, m_iByte, iPassedTimeUs, m_eNetStaticsticType, iRetCode);
}

NFModuleWatchTime::NFModuleWatchTime(int iID, const std::string szName)
{
	m_iID = iID;
	m_szName = szName;
	if (ms_bNeedRecordTime)
	{
		m_stBegin = NFGetTime();
	}
}

NFModuleWatchTime::~NFModuleWatchTime()
{
	if (ms_bNeedRecordTime)
	{
		int iPassedTimeUs = NFGetTime() - m_stBegin;

		if (iPassedTimeUs < 0)
		{
			LOGSVR_ERROR("iPassedTimeUs Is Invalid:" << iPassedTimeUs << " ID:" << m_iID);
			iPassedTimeUs = 0;
		}

		NFOperatingStatistcsMgr::Instance()->AddModulaStat(m_iID, m_szName, iPassedTimeUs);
	}
}
